package com.yyf.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyf.blogapi.po.Comment;
import com.yyf.blogapi.mapper.CommentMapper;
import com.yyf.blogapi.po.SysUser;
import com.yyf.blogapi.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyf.blogapi.service.ISysUserService;
import com.yyf.blogapi.util.UserThreadLocal;
import com.yyf.blogapi.vo.CommentParam;
import com.yyf.blogapi.vo.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yyf
 * @since 2021-08-16
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    CommentMapper commentMapper;
    @Resource
    ISysUserService userService;

    @Override
    public List<CommentVo> getCommentsByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId, articleId);
        queryWrapper.eq(Comment::getLevel, 1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }

    @Override
    public Void saveComment(CommentParam commentParam) {
        //只有加入到拦截的范围中，才能在ThreadLocal中获取到user
        SysUser user = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setAuthorId(user.getId());
        comment.setContent(commentParam.getContent());
        comment.setArticleId(commentParam.getArticleId());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        } else {
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentMapper.insert(comment);
        return null;
    }

    @Override
    public int commentsCount(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId, id);
        return commentMapper.selectCount(queryWrapper);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : comments) {
            commentVos.add(copy(comment));
        }
        return commentVos;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        //作者信息
        commentVo.setAuthor(userService.getById(comment.getAuthorId()));
        //子评论
        if (comment.getLevel() == 1) {
            List<CommentVo> childrenComments = getChildrenCommentsByParentId(comment.getId());
            commentVo.setChildrens(childrenComments);
        }
        //给谁评论
        if (comment.getLevel() > 1) {
            commentVo.setToUser(userService.getById(comment.getToUid()));
        }
        return commentVo;
    }

    private List<CommentVo> getChildrenCommentsByParentId(Long parentId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId, parentId);
        queryWrapper.eq(Comment::getLevel, 2);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        return copyList(comments);
    }
}
